//
//  HeartVideoViewController.m
//  miaopai
//
//  Created by duyifang on 15/11/23.
//  Copyright © 2015年 Crow Zhang. All rights reserved.
//

#import "HeartVideoViewController.h"
#import "HeartVideoLayout.h"
#import "HeartVideoCollectionViewCell.h"
#import "CoreData.h"
#import "AutoadaptationFrame.h"
#import "VideoCollectModel.h"
#import "VideoDetailViewController.h"
@interface HeartVideoViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, HeartVideoLayoutDelegate>

@property(nonatomic, retain) UICollectionView *collectionView;

@property(nonatomic, retain) CoreData *coreDataManager;

@property(nonatomic, retain) NSArray *modelArray;

@end

@implementation HeartVideoViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self getCoreDataModel];
    [_collectionView reloadData];
    [self creatNoCollectView];
    
    [super viewWillAppear:YES];
    UIView * view = [self.tabBarController.view.subviews lastObject];
    [UIView animateWithDuration:0.5 animations:^{
        view.frame = CGRectMake(0, kHeight + 100, kWidth, 48);
    }];
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"红心视频";
    [self getCoreDataModel];
    [self creatSubView];
}

- (void)getCoreDataModel
{
    self.coreDataManager = [CoreData shareManagerWithFile:@"joke"];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"VideoCollectModel" inManagedObjectContext:_coreDataManager.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSError *error = nil;
    NSArray *fetchedObjects = [_coreDataManager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"%@", error);
    }else
    {
        self.modelArray = fetchedObjects;
    }
}

- (void)creatSubView
{
    HeartVideoLayout *layout = [[HeartVideoLayout alloc] init];
    layout.delegate = self;
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    [layout release];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[HeartVideoCollectionViewCell class] forCellWithReuseIdentifier:@"HeartVideoCollectionViewCell"];
    _collectionView.backgroundColor = [UIColor colorWithRed:0.9063 green:0.9063 blue:0.9063 alpha:1.0];
    [self.view addSubview:_collectionView];
    [_collectionView release];
}

- (void)creatNoCollectView
{
    if (_modelArray.count == 0) {
        UILabel *noModelLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 75, self.view.frame.size.width, 20)];
        noModelLabel.text = @"当前还没有收藏的视频";
        noModelLabel.textColor = [UIColor colorWithRed:0.5709 green:0.5709 blue:0.5709 alpha:1.0];
        noModelLabel.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:noModelLabel];
        [noModelLabel release];
    }
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _modelArray.count;
}

- (CGSize)collectionview:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtItemIndexPath:(NSIndexPath *)indexPath
{
    VideoCollectModel *videoCollectModel = [_modelArray objectAtIndex:indexPath.row];
    CGFloat itemWidth = (self.view.frame.size.width - 30) / 2;
    if ([videoCollectModel.describe isEqualToString:@""]) {
        CGFloat pictureHeight = itemWidth;
        return CGSizeMake(itemWidth, pictureHeight + 5);
    }else
    {
        CGFloat pictureHeight = itemWidth + 20;
        CGFloat wordsHeight = [AutoadaptationFrame heightOfLabel:videoCollectModel.describe andTextFont:[UIFont systemFontOfSize:13] andLabelWidth:itemWidth - 10];
        return CGSizeMake(itemWidth, pictureHeight + wordsHeight + 20);
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HeartVideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HeartVideoCollectionViewCell" forIndexPath:indexPath];
    cell.videoCollectModel = [_modelArray objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    cell.layer.cornerRadius = 5;
    cell.layer.masksToBounds = YES;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    VideoDetailViewController *videoDetailViewController = [[VideoDetailViewController alloc] init];
    videoDetailViewController.scid = [[_modelArray objectAtIndex:indexPath.row] scid];
    [self.navigationController pushViewController:videoDetailViewController animated:YES];
    [videoDetailViewController release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
